//author:kakake
class tssyncdataDao {
    public data = {
        dbConn: TSLobsterAPI.Program.GetDbConn(),
        dbConnBoot: TSLobsterAPI.Program.GetDbConn("LobsterBoot"),
        WorkId: 1
    }

    constructor(workid: number) {
        this.data.WorkId = workid;
    }

    /**获取SSO平台的机构信息 */
    getSSOWork(currworkid) {
        let strsql = "select * from baseworkers where workid=@currworkid";
        return TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConnBoot, strsql, { currworkid: currworkid });
    }

    /**判断机构是否在HIS中存在 */
    existHISWork(currworkid): boolean {
        let strsql = "select count(1) from baseworkers where workid=@currworkid";
        var ret = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, { currworkid: currworkid });
        return parseInt(ret) > 0;
    }

    /**插入HIS机构，自增长ID也要插入 */
    insertHISWork(ssowork) {
        let strsql = "SET IDENTITY_INSERT BaseWorkers ON ;\
                    INSERT INTO BaseWorkers([WorkId], [WorkNo], [WorkName], [Memo], [RegKey], [EditionCode], [DelFlag]) VALUES (@WorkId, @WorkNo, @WorkName, @Memo, @RegKey, @EditionCode, 0);\
                    SET IDENTITY_INSERT BaseWorkers OFF ;";
        var dbparams = {
            WorkId: ssowork.WorkId,
            WorkNo: ssowork.WorkNo,
            WorkName: ssowork.WorkName,
            Memo: ssowork.Memo,
            RegKey: ssowork.RegKey,
            EditionCode: ssowork.EditionCode
        }

        return TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, dbparams);
    }

    /**获取机构管理员 */
    getSSOUser(ssouserid, currworkid) {
        let strsql = "select * from sso_user where userid=@ssouserid";
        var user = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConnBoot, strsql, { ssouserid: ssouserid });

        let strsql2 = "SELECT * FROM BaseEmployee WHERE WorkID=@currworkid and Telephone=@Telephone";
        var emp = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConnBoot, strsql2, { currworkid: currworkid, Telephone: user.Telephone });

        return { user: user, emp: emp };
    }

    /**判断用户是否绑定了HIS用户 */
    existSSOUser(ssouserid, currworkid) {
        let strsql = "SELECT count(1) FROM SSO_UserScanQrBind WHERE Status=0 and SSOUserID=@ssouserid and WorkID=@currworkid";
        var ret = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConnBoot, strsql, { currworkid: currworkid, ssouserid: ssouserid });
        return parseInt(ret) > 0;
    }

    /**SSO用户绑定HIS用户 */
    bindSSOUser(ssouserid, currworkid, hiscode) {
        let strsql = "INSERT INTO SSO_UserScanQrBind([Type], [SSOUserId], [WorkId], [BindUserCode], [Status], [CreateTime]) VALUES (1, @ssouserid, @currworkid, @code, 0, GETDATE())";
        let dbparams = {
            ssouserid: ssouserid,
            currworkid: currworkid,
            code: hiscode
        }
        TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConnBoot, strsql, dbparams);
    }

    /**判断机构用户是否存在 */
    existHISUser(currworkid, code, name, telephone) {
        let strsql = "SELECT count(1) FROM BaseUser a LEFT JOIN BaseEmployee b on a.EmpID=b.EmpId where a.WorkId=@currworkid and a.Code=@code and b.Name=@name";
        let dbparams = {
            currworkid: currworkid,
            code: code,
            name: name
        }
        var ret = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, dbparams);
        return parseInt(ret) > 0;
    }

    /**插入HIS机构管理员 */
    insertHISUser(currworkid, ssouser, empid) {
        // let strsql = "INSERT INTO BaseEmployee([Name], [Sex], [Brithday], [Szm], [Pym], [Wbm], [DelFlag], [WorkId]) VALUES (@Name, @Sex, @Brithday, @Telephone, '', '', 0, @currworkid);SELECT @@IDENTITY;";
        // let dbparams = {
        //     Name: ssouser.emp.Name,
        //     Sex: ssouser.emp.Sex,
        //     Brithday: ssouser.emp.Brithday,
        //     Telephone: ssouser.emp.Telephone,
        //     currworkid: currworkid
        // }

        // var empid = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, dbparams);

        let strsql2 = "INSERT INTO BaseUser([EmpID], [Code], [PassWord], [GroupId], [Lock], [Memo], [UserType], [DoctorPost], [NursePost], [IsAdmin], [WorkId]) VALUES (@empid, @Code, @PassWord, 0, 0, '', 1, '0', '0', @IsAdmin, @currworkid);";
        let dbparams2 = {
            empid: parseInt(empid),
            Code: ssouser.user.UserCode,
            PassWord: TSLobsterAPI.Utils.DesEncrypt("1"),
            IsAdmin: ssouser.user.IsAdmin,
            currworkid: currworkid
        }
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql2, dbparams2);
    }

    /**获取机构人员 */
    getSSOEmpList(currworkid) {
        let strsql = "SELECT * FROM BaseEmployee where WorkId=@currworkid";
        return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConnBoot, strsql, { currworkid: currworkid });
    }

    /**获取平台人员所属科室 */
    getSSOEmpDept(currworkid, ssoempid) {
        let strsql = "SELECT b.Name,a.DefaultFlag FROM BaseEmpDept a LEFT JOIN BaseDept b on a.WorkId=b.WorkId and a.DeptId=b.DeptId WHERE a.WorkId=@currworkid and a.EmpId=@ssoempid";
        return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConnBoot, strsql, { currworkid: currworkid, ssoempid: ssoempid });
    }

    /**获取HIS科室id */
    getHISDeptid(currworkid,name){
        let strsql="SELECT top 1 DeptId FROM BaseDept where WorkId=@currworkid and Name=@name";
        var ret = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, {currworkid:currworkid,name:name});
        return parseInt(ret);
    }

    /**判断人员 */
    existHISEmp(currworkid, name, telephone) {
        let strsql = "SELECT count(1) FROM BaseEmployee where Name=@name and Szm=@telephone and WorkId=@currworkid";
        let dbparams = {
            name: name,
            telephone: telephone,
            currworkid: currworkid
        }
        var ret = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, dbparams);
        return parseInt(ret) > 0;
    }

    /**获取HIS empid */
    getHISEmpid(currworkid, name, telephone) {
        let strsql = "SELECT EmpId FROM BaseEmployee where Name=@name and Szm=@telephone and WorkId=@currworkid";
        let dbparams = {
            name: name,
            telephone: telephone,
            currworkid: currworkid
        }
        var ret = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, dbparams);
        return parseInt(ret);
    }

    /**插入人员 */
    insertHISEmp(currworkid, ssoemp): number {
        let strsql = "INSERT INTO BaseEmployee([Name], [Sex], [Brithday], [Szm], [Pym], [Wbm], [DelFlag], [WorkId]) VALUES (@Name, @Sex, @Brithday, @Telephone, '', '', 0, @currworkid);SELECT @@IDENTITY;";
        let dbparams = {
            Name: ssoemp.Name,
            Sex: ssoemp.Sex,
            Brithday: ssoemp.Brithday,
            Telephone: ssoemp.Telephone,
            currworkid: currworkid
        }

        var empid = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, dbparams);

        return parseInt(empid);
    }

    /**插入人员所属科室 */
    insertHISEmpDept(currworkid,empid,deptid,defaultflag){
        let strsql="INSERT INTO [BaseEmpDept]([EmpId], [DeptId], [DefaultFlag], [WorkId]) VALUES (@empid, @deptid, @defaultflag, @currworkid);";
        let dbparams={
            currworkid:currworkid,
            empid:empid,
            deptid:deptid,
            defaultflag:defaultflag
        }
        TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, dbparams);

    }

    /**更新平台的hisempid */
    updateSSOhisempid(currworkid, ssoempid, hisempid) {
        let strsql = "UPDATE BaseEmployee set HIS_EmpId=@hisempid WHERE WorkId=@currworkid and EmpId=@ssoempid";
        let dbparams = {
            currworkid: currworkid,
            ssoempid: ssoempid,
            hisempid: hisempid
        }
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConnBoot, strsql, dbparams);
    }

    /**获取平台的科室分类 */
    getSSODeptlayer(currworkid) {
        let strsql = "SELECT * FROM BaseBranch where WorkId=@currworkid";
        var ssobranch = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConnBoot, strsql, { currworkid: currworkid });

        let strsql2 = "SELECT * FROM BaseDeptLayer Where WorkId=@currworkid";
        var ssodeptlayerlist = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConnBoot, strsql2, { currworkid: currworkid });

        return { ssobranch: ssobranch, ssodeptlayerlist: ssodeptlayerlist };
    }

    /**获取平台科室 */
    getSSODept(currworkid) {
        let strsql = "SELECT a.*,b.Name LayerName FROM BaseDept a LEFT JOIN BaseDeptLayer b on a.WorkId=b.WorkId and a.Layer=b.LayerId WHERE a.WorkId=@currworkid";
        return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConnBoot, strsql, { currworkid: currworkid });
    }

    /**判断科室分类是否存在，存在就不同步 */
    existHISDeptlayer(currworkid) {
        let strsql = "SELECT count(1) FROM BaseDeptLayer where WorkId=@currworkid";
        var ret = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, { currworkid: currworkid });
        return parseInt(ret) > 0;
    }

    /**插入科室分类 */
    insertHISDeptlayer(currworkid, ssobranch, ssodeptlayerlist) {
        let strsql = "INSERT INTO BaseDeptLayer([PId], [Name], [WorkId]) VALUES (0, @name, @currworkid);SELECT @@IDENTITY;";
        let dbparams = {
            currworkid: currworkid,
            name: ssobranch.BranchHospitalName
        }
        var layerid = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, dbparams);

        ssodeptlayerlist.forEach(item => {
            let strsql2 = "INSERT INTO BaseDeptLayer([PId], [Name], [WorkId]) VALUES (@pid, @name, @currworkid)";
            let dbparams2 = {
                currworkid: currworkid,
                pid: layerid,
                name: item.Name
            }

            TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql2, dbparams2);
        })
    }

    /**判断HIS科室是否存在 */
    existHISDept(currworkid, name) {
        let strsql = "SELECT count(1) FROM BaseDept where WorkId=@currworkid and Name=@name";
        var ret = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, { currworkid: currworkid, name: name });
        return parseInt(ret) > 0;
    }

    /**插入HIS科室 */
    insertHISDept(currworkid, ssodept) {
        let strsql = "SELECT top 1 * FROM BaseDeptLayer where WorkId=@currworkid";
        var defaultdeptlayer = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, { currworkid: currworkid });

        strsql = "SELECT top 1 * FROM BaseDeptLayer where WorkId=@currworkid and Name=@layername";
        var deptlayer = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, { currworkid: currworkid, layername: ssodept.LayerName });

        var layerid = deptlayer ? deptlayer.LayerId : defaultdeptlayer.LayerId;
        strsql = "INSERT INTO [BaseDept]([Layer], [Name], [Pym], [Wbm], [Szm], [Code], [DelFlag], [SortOrder], [Memo], [WorkId]) \
        VALUES (@layerid, @Name, @Pym, @Wbm, @Szm, @Code, @DelFlag, @SortOrder, @Memo, @currworkid);\
        SELECT @@IDENTITY;"
        var dbparams = {
            layerid: layerid,
            Name: ssodept.Name,
            Pym: ssodept.Pym,
            Wbm: ssodept.Wbm,
            Szm: ssodept.Szm,
            Code: ssodept.Code,
            DelFlag: ssodept.DelFlag,
            SortOrder: ssodept.SortOrder,
            Memo: ssodept.Memo,
            currworkid: currworkid
        }
        var deptid = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, dbparams);
        return parseInt(deptid);
    }

    /**更新平台科室的hisdeptid */
    updateSSOhisdeptid(currworkid, ssodeptid, hisdeptid) {
        let strsql = "UPDATE BaseDept set HIS_DeptId=@hisdeptid where WorkId=@currworkid and DeptId=@ssodeptid";
        let dbparams = {
            hisdeptid: hisdeptid,
            ssodeptid: ssodeptid,
            currworkid: currworkid
        }
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConnBoot, strsql, dbparams);
    }
}